/*     */ package com.zimbra.cs.mailbox.alerts;
/*     */ 
/*     */ import com.zimbra.common.calendar.ParsedDateTime;
/*     */ import com.zimbra.common.calendar.ParsedDuration;
/*     */ import com.zimbra.common.mime.shim.JavaMailInternetAddress;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.L10nUtil;
/*     */ import com.zimbra.common.util.L10nUtil.MsgKey;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.common.zmime.ZMimeBodyPart;
/*     */ import com.zimbra.common.zmime.ZMimeMultipart;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailSender;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import com.zimbra.cs.mailbox.calendar.Util;
/*     */ import com.zimbra.cs.mailbox.calendar.ZOrganizer;
/*     */ import com.zimbra.cs.mime.Mime.FixedMimeMessage;
/*     */ import com.zimbra.cs.util.JMSession;
/*     */ import java.text.DateFormat;
/*     */ import java.util.Date;
/*     */ import java.util.Locale;
/*     */ import java.util.TimeZone;
/*     */ import javax.mail.Message.RecipientType;
/*     */ import javax.mail.internet.MimeBodyPart;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.mail.internet.MimeMultipart;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CalItemEmailReminderTask
/*     */   extends CalItemReminderTaskBase
/*     */ {
/*     */   static final String TASK_NAME_PREFIX = "emailReminderTask";
/*     */   
/*     */   public String getName()
/*     */   {
/*  57 */     return "emailReminderTask" + getProperty("calItemId");
/*     */   }
/*     */   
/*     */   protected void sendReminder(CalendarItem calItem, Invite invite) throws Exception
/*     */   {
/*  62 */     Account account = calItem.getAccount();
/*  63 */     Locale locale = account.getLocale();
/*  64 */     TimeZone tz = Util.getAccountTimeZone(account);
/*     */     
/*  66 */     MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSmtpSession(account));
/*     */     
/*  68 */     String to = account.getAttr("zimbraPrefCalendarReminderEmail");
/*  69 */     if (to == null) {
/*  70 */       ZimbraLog.scheduler.info("Unable to send calendar reminder email since %s is not set", new Object[] { "zimbraPrefCalendarReminderEmail" });
/*  71 */       return;
/*     */     }
/*  73 */     mm.setRecipient(Message.RecipientType.TO, new JavaMailInternetAddress(to));
/*     */     
/*  75 */     mm.setSubject(L10nUtil.getMessage(calItem.getType() == MailItem.Type.APPOINTMENT ? L10nUtil.MsgKey.apptReminderEmailSubject : L10nUtil.MsgKey.taskReminderEmailSubject, locale, new Object[] { calItem.getSubject() }), "utf-8");
/*     */     
/*     */ 
/*     */ 
/*  79 */     if (invite.getDescriptionHtml() == null) {
/*  80 */       mm.setText(getBody(calItem, invite, false, locale, tz), "utf-8");
/*     */     } else {
/*  82 */       MimeMultipart mmp = new ZMimeMultipart("alternative");
/*  83 */       mm.setContent(mmp);
/*     */       
/*  85 */       MimeBodyPart textPart = new ZMimeBodyPart();
/*  86 */       textPart.setText(getBody(calItem, invite, false, locale, tz), "utf-8");
/*  87 */       mmp.addBodyPart(textPart);
/*     */       
/*  89 */       MimeBodyPart htmlPart = new ZMimeBodyPart();
/*  90 */       htmlPart.setContent(getBody(calItem, invite, true, locale, tz), "text/html; charset=utf-8");
/*  91 */       mmp.addBodyPart(htmlPart);
/*     */     }
/*     */     
/*  94 */     mm.setSentDate(new Date());
/*     */     
/*  96 */     mm.saveChanges();
/*     */     
/*  98 */     MailSender mailSender = calItem.getMailbox().getMailSender();
/*  99 */     mailSender.sendMimeMessage(null, calItem.getMailbox(), mm);
/*     */   }
/*     */   
/*     */   private String getBody(CalendarItem calItem, Invite invite, boolean html, Locale locale, TimeZone tz) throws ServiceException {
/* 103 */     DateFormat dateTimeFormat = DateFormat.getDateTimeInstance(3, 3, locale);
/* 104 */     dateTimeFormat.setTimeZone(tz);
/* 105 */     DateFormat onlyDateFormat = DateFormat.getDateInstance(3, locale);
/* 106 */     onlyDateFormat.setTimeZone(tz);
/* 107 */     DateFormat onlyTimeFormat = DateFormat.getTimeInstance(3, locale);
/* 108 */     onlyTimeFormat.setTimeZone(tz);
/*     */     String formattedEnd;
/*     */     String formattedStart;
/*     */     String formattedEnd;
/* 112 */     if (calItem.getType() == MailItem.Type.APPOINTMENT) {
/* 113 */       Date start = new Date(new Long(getProperty("nextInstStart")).longValue());
/* 114 */       String formattedStart = dateTimeFormat.format(start);
/* 115 */       Date end = invite.getEffectiveDuration().addToDate(start);
/* 116 */       formattedEnd = onlyDateFormat.format(start).equals(onlyDateFormat.format(end)) ? onlyTimeFormat.format(end) : dateTimeFormat.format(end);
/*     */     }
/*     */     else {
/* 119 */       formattedStart = invite.getStartTime() == null ? "" : onlyDateFormat.format(invite.getStartTime().getDate());
/* 120 */       formattedEnd = invite.getEndTime() == null ? "" : onlyDateFormat.format(invite.getEndTime().getDate());
/*     */     }
/*     */     
/* 123 */     String location = invite.getLocation();
/*     */     
/* 125 */     String organizer = null;
/* 126 */     ZOrganizer zOrganizer = invite.getOrganizer();
/* 127 */     if (zOrganizer != null) {
/* 128 */       organizer = zOrganizer.hasCn() ? zOrganizer.getCn() : zOrganizer.getAddress();
/*     */     }
/* 130 */     if (organizer == null) {
/* 131 */       organizer = "";
/*     */     }
/*     */     
/*     */ 
/* 135 */     String folder = calItem.getMailbox().getFolderById(null, calItem.getFolderId()).getName();
/*     */     
/* 137 */     String description = html ? invite.getDescriptionHtml() : invite.getDescription();
/* 138 */     if (description == null) { description = "";
/*     */     }
/* 140 */     return html ? L10nUtil.getMessage(calItem.getType() == MailItem.Type.APPOINTMENT ? L10nUtil.MsgKey.apptReminderEmailBodyHtml : L10nUtil.MsgKey.taskReminderEmailBodyHtml, locale, new Object[] { formattedStart, formattedEnd, location, organizer, folder, description }) : L10nUtil.getMessage(calItem.getType() == MailItem.Type.APPOINTMENT ? L10nUtil.MsgKey.apptReminderEmailBody : L10nUtil.MsgKey.taskReminderEmailBody, locale, new Object[] { formattedStart, formattedEnd, location, organizer, folder, description });
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/alerts/CalItemEmailReminderTask.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */